package com.qin.malldiscount.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.malldiscount.domain.SmsHomeAdvertise;
import com.qin.malldiscount.service.SmsHomeAdvertiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页轮播广告管理
 * @author Leisure
 * create 2022 2022/11/24 15:19
 **/
@RestController
@RequestMapping("/home/advertiseManage")
public class SmsHomeAdvertiseController {
    
    @Autowired
    private SmsHomeAdvertiseService advertiseService;

    /**
     * 添加广告
     */
    @PostMapping("/create")
    @ResponseBody
    public CommonResult create(@RequestBody SmsHomeAdvertise advertise) {
        int count = advertiseService.create(advertise);
        if (count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    /**
     * 删除广告
     */
    @PostMapping("/delete")
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = advertiseService.delete(ids);
        if (count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    /**
     * 修改上下线状态
     */
    @PostMapping("/update/status/{id}")
    @ResponseBody
    public CommonResult updateStatus(@PathVariable Long id,@RequestParam Integer status) {
        int count = advertiseService.updateStatus(id, status);
        if (count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    /**
     * 获取广告详情
     */
    @GetMapping("/{id}")
    @ResponseBody
    public CommonResult<SmsHomeAdvertise> getItem(@PathVariable Long id) {
        SmsHomeAdvertise advertise = advertiseService.getItem(id);
        return CommonResult.success(advertise);
    }
    
    /**
    *  修改广告
     */
    @PostMapping("/update/{id}")
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody SmsHomeAdvertise advertise) {
        int count = advertiseService.update(id, advertise);
        if (count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    /**
     * 分页查询广告
     */
    @GetMapping("/list")
    @ResponseBody
    public CommonResult<MyBatisPlusPage<SmsHomeAdvertise>> list(@RequestParam(value = "name", required = false) String name,
                                                                @RequestParam(value = "type", required = false) Integer type,
                                                                @RequestParam(value = "endTime", required = false) String endTime,
                                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<SmsHomeAdvertise> advertiseList = advertiseService.list(name, type, endTime, pageSize, pageNum);
        return CommonResult.success(MyBatisPlusPage.mpPage(advertiseList));
    }

    
}
